package
{
    import flash.display.BlendMode;
    import flash.display.Shape;
    import flash.display.Sprite;

    /**
     *
     * @author jjung
     */
    public class Mitosis
    {
        //---------------------------------------------------------------------
        //
        //  Static Variables
        //
        //---------------------------------------------------------------------
        /**
        * The amount the cells move apart when doing mitosis.
        */
        public static var MITOSIS_DISTANCE : Number = 7;

        public static var MITOSIS_TIME : Number = 1.0;

        //---------------------------------------------------------------------
        //
        //  Variables
        //
        //---------------------------------------------------------------------
        // Microbe 1 is our root microbe... the one that is spawning microbe2
        public var microbe1 : Microbe;
        public var microbe2 : Microbe;

        public var mitosisSprite : Sprite = null;
        public var edgeShape : Shape = null;
        public var fillShape : Shape = null;

        // Number from 0 to 1 showing progress of mitosis.
        public var progress : Number = 0.0;

        // The angle at which the mitosis occurs. Basically this is the direction in which the
        // new microbe will split from the original.
        public var angle : Number = 0.0;
        public var sin : Number = 0.0;
        public var cos : Number = 0.0;

        //---------------------------------------------------------------------
        //
        //  Constructor
        //
        //---------------------------------------------------------------------
        /**
         *  Constructor
         */
        public function Mitosis(microbe1 : Microbe, microbe2 : Microbe)
        {
            this.microbe1 = microbe1;
            this.microbe2 = microbe2;

            startMitosis();
        }

        //---------------------------------------------------------------------
        //
        //  Methods
        //
        //---------------------------------------------------------------------
        public function startMitosis()
        {
            // First thing, make microbes invisible for time being.
            microbe1.visible = false;
            microbe2.visible = false;

            microbe1.mitosis = microbe2.mitosis = true;

            MicrobialLife.getInstance().mitosises.push(this);

            mitosisSprite = new Sprite();
            mitosisSprite.x = microbe1.x;
            mitosisSprite.y = microbe1.y;

            edgeShape = new Shape();
            fillShape = new Shape();

            mitosisSprite.addChild(edgeShape);
            mitosisSprite.addChild(fillShape);

            angle = Math.random() * Math.PI;
            cos = Math.cos(angle);
            sin = Math.sin(angle);

            MicrobialLife.getInstance().scratchy.addChild(mitosisSprite);
        }

        public function update(timeElapsed : Number)
        {
            progress += timeElapsed;

            microbe1.nutrientMass -= (timeElapsed * (1 / MITOSIS_TIME)) * microbe1.nutrientMass;
            microbe1.toxinMass -= (timeElapsed * (1 / MITOSIS_TIME)) * microbe1.toxinMass;

            microbe2.nutrientMass -= (timeElapsed * (1 / MITOSIS_TIME)) * microbe2.nutrientMass;
            microbe2.toxinMass -= (timeElapsed * (1 / MITOSIS_TIME)) * microbe2.toxinMass;

            if (progress > MITOSIS_TIME)
            {
                // End of mitosis...
                microbe1.x -= MITOSIS_DISTANCE * cos;
                microbe1.y -= MITOSIS_DISTANCE * sin

                microbe2.x += MITOSIS_DISTANCE * cos;
                microbe2.y += MITOSIS_DISTANCE * sin;

                MicrobialLife.getInstance().microbes.push(microbe2);

                microbe1.visible = microbe2.visible = true;
                microbe1.mitosis = microbe2.mitosis = false;

                microbe2.selected = microbe1.selected;
                microbe2.destination = microbe1.destination;

                microbe1.resetRender();
                microbe2.resetRender();

                microbe1.resetPhysics();
                microbe2.resetPhysics();

                MicrobialLife.getInstance().scratchy.removeChild(mitosisSprite);
                MicrobialLife.getInstance().mitosisesToSplice.push(this);
            }
        }

        public function render() : void
        {
            edgeShape.graphics.clear();
            fillShape.graphics.clear();

            // Basically we want to split the area of the microbe... which means going from an
            // area of PI * 20^2 to an area of half that... or basically radius of sqrt(200) = 14.14.
            // So we slowly decrease the size by the difference between 20 and 14.14 = 5.6.
            var radius : Number = microbe1.radius;
            var maskRadius : Number = radius - 1;

            var cosModifier : Number = progress * MITOSIS_DISTANCE * cos;
            var sinModifier : Number = progress * MITOSIS_DISTANCE * sin;

            // Microbe 1 FILL
            fillShape.graphics.beginFill(microbe1.fillColor, 1.0);
            fillShape.graphics.drawCircle(-cosModifier, -sinModifier, maskRadius);
            fillShape.graphics.endFill();

            // Microbe 2 FILL
            fillShape.graphics.beginFill(microbe1.fillColor, 1.0);
            fillShape.graphics.drawCircle(cosModifier, sinModifier, maskRadius);
            fillShape.graphics.endFill();

            // Microbe 1 MASK
            edgeShape.graphics.beginFill(0xFFFFFF, 1.0);
            edgeShape.graphics.drawCircle(-cosModifier, -sinModifier, radius);
            edgeShape.graphics.endFill();

            // MIcrobe 2 MASK
            edgeShape.graphics.beginFill(0xFFFFFF, 1.0);
            edgeShape.graphics.drawCircle(cosModifier, sinModifier, radius);
            edgeShape.graphics.endFill();

            if (microbe1.selected)
            {
                fillShape.graphics.lineStyle(1, 0xFFFFFF, 0.8);
                fillShape.graphics.drawRect(-radius - cosModifier, -radius - sinModifier, microbe1.diameter, microbe1.diameter);
                fillShape.graphics.drawRect(-radius + cosModifier, -radius + sinModifier, microbe2.diameter, microbe2.diameter);
            }
        }
    }
}